package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ArticleDAO;
import dao.ContentDAO;
import entity.Article;

/**
 * Servlet implementation class ModifyServlet
 */
@WebServlet("/ModifyArticleServlet")
public class ModifyArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArticleDAO articleDAO = ArticleDAO.getInstance();
	private ContentDAO  contentDAO= ContentDAO.getInstance();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		try{
			int article_id=Integer.parseInt((String)request.getParameter("article_id"));
			String content = (String)request.getParameter("editorValue");
			int theme_id = Integer.parseInt((String)request.getParameter("selected_theme"));//选择的主题
			String article_title = (String)request.getParameter("Article_title");//帖子名
			//System.out.println(article_title);
			Article article=articleDAO.get(article_id);
			article.setArticle_title(article_title);
			article.setTheme_id(theme_id);
			articleDAO.update(article, content);
			//Content content = new Content(2,"aaaaaaaaa");
			//contentDAO.add(content);
			//int article_id=articleDAO.add(article_title,1,theme_id,content);//添加了帖子到数据库
			//System.out.println("article的id"+article_id);
			request.setAttribute("article_id", article_id);
			request.setAttribute("type",2);//modify的type设为2

			String fromadmin = (String)request.getParameter("from");
			if(fromadmin!=null&&fromadmin.equals("admin")){//从后台来的请
				request.getRequestDispatcher("BeforeModityArticleServlet?article_id="+article_id+"&from=admin").forward(request, response);
			}
			else {
				request.getRequestDispatcher("AfterAdd_success.jsp").forward(request, response);
			}
		}catch(Exception e){
			//request.getRequestDispatcher("AfterAdd_fail.jsp").forward(request, response);
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
